#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "rngs.h"
#include "math.h"

void prepRandom(int strm, int seed){
  SelectStream(strm);
  PutSeed((long)seed);
}

int getRandomNumber(int min, int max){
	int number = floor(Random() * (max-min));
	number += min;
	return number;

}

int randomizeDeck(struct gameState *g){
	int i, j, deckSize, card;
	for (i = 0; i < g->numPlayers; i++){
		deckSize = getRandomNumber(0, MAX_DECK);
		g->deckCount[i] = deckSize;
	//	printf("number of Players: %d\n", g->numPlayers);
	//	printf("Player %d, deck size: %d\n", i, g->deckCount[i]);
		for(j=0; j < deckSize; j++){
			card = getRandomNumber(0, 26);
			g->deck[i][j] = card;
	//		printf("[Player %d, card %d = %d] ", i, j, g->deck[i][j]);
		}
	//	printf("\n\n");
	}
	return 0;
}

int makeRandomGameState(struct gameState *g){
	g->numPlayers = getRandomNumber(2, 4);
	randomizeDeck(g);
	return 0;
}

int testShuffle(struct gameState *g){
	//needs: player number, gamestate attr: deckCount[player], deck[player]
	//player's deck should have same cards before and after shuffle
	int i;
	int player; 
	
	for(player = 0; player < g->numPlayers; player ++){
		int cardsInDeck_A[27];
		int deckCount_A = g->deckCount[player];
		for (i=0; i< 27; i++){
			cardsInDeck_A[i] = 0;
		}
		for(i=0; i < deckCount_A; i++){
			if(g->deck[player][i] == i){
				cardsInDeck_A[i]++;
			}
		}
		shuffle(player, g);
		int cardsInDeck_B[27];
		int deckCount_B = g->deckCount[player];
		for (i=0; i< 27; i++){
			cardsInDeck_B[i] = 0;
		}
		for(i=0; i < deckCount_B; i++){
			if(g->deck[player][i] == i){
				cardsInDeck_B[i]++;
			}
		}
		if(deckCount_A != deckCount_B){
			printf("Deck count not the same!\n");
		}
		for(i=0; i < 27; i++){
			if(cardsInDeck_A[i] != cardsInDeck_B[i]){
					printf("Count for Card %d not equal! Before: %d After: %d Player %d\n", i, cardsInDeck_A[i], cardsInDeck_B[i], player);
			}
		}
	}
	printf("Test Complete\n");

	return 1;
}

void printG(struct gameState *g){
	printf("number of players: %d\n", g->numPlayers);


}

int main(int argc, char** argv){
	int strm = 1, seed = 4, min = 2, max = 50;
	if(argc > 1){
		strm = atoi(argv[1]);
	}
	if(argc > 2){
		seed = atoi(argv[2]);
	}
	if(argc > 3){
		min = atoi(argv[3]);
	}
	if(argc > 4){
		max = atoi(argv[4]);
	}
	prepRandom(strm, seed);
	//int i, output;
	//for(i=0; i < 10; i++){
	//	output = getRandomNumber(min, max);
	//	printf("output = %d\n", output);
	//}
	int i;
	for(i=0; i < 100; i++){
	
		struct gameState *g = newGame();
		makeRandomGameState(g);
		testShuffle(g);
	}



	return 0;
}

